package com.gohi.main.user.service.impl;



import java.util.ArrayList;
import java.util.List;

import org.hibernate.TransactionException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import com.gohi.core.service.impl.DefaultService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.user.hbm.ShopgooMessage;
import com.gohi.main.user.hbm.jdbcMapper.MemberMapper;
import com.gohi.main.user.service.IUserService;
import com.gohi.main.util.MD5;
import com.gohi.main.util.StringUtil;



public class UserServiceImpl extends DefaultService<Object, String> implements IUserService {
	@SuppressWarnings("unchecked")
	@Override
	public ShopgooMember Login(String username, String userpass)
			throws TransactionException {
		userpass=MD5.MD5Encode(userpass);//加密
		String sql ="SELECT  * from shopgoo_member where member_name='"+username+"' and password='"+userpass+"'";
		//System.err.println(sql);
		JdbcTemplate jb =getDAO().getJdbcTemplate();
		List<ShopgooMember> list=  jb.query(sql, new MemberMapper());
		if(list.size()==0){
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public ShopgooMember getById(String id) throws TransactionException {
		String sql ="SELECT  * from shopgoo_member where member_id='"+id+"'";
		JdbcTemplate jb =getDAO().getJdbcTemplate();
		List<ShopgooMember> list=  jb.query(sql, new MemberMapper());
		if(list.size()==0){
			return null;
		}
		return list.get(0);
	}

	@Override
	public int getByNames(String name) throws TransactionException {
		// TODO Auto-generated method stub
		String sql ="SELECT  count(*) from shopgoo_member where member_name='"+name+"'";
		JdbcTemplate jb =getDAO().getJdbcTemplate();
		return jb.queryForInt(sql);
	}

	@Override
	public int regMember(ShopgooMember user) throws TransactionException {
		// TODO Auto-generated method stub
		//System.err.println(StringUtil.getTimeSimple());
		
		
		String sql ="insert into shopgoo_member (member_name,password,nickname,gender,email,mobile,avatar,usercity,register_time,pay_password,member_degree,member_point,member_contribution,province,city,district) " +
				"values (?,?,?,0,?,?,?,'67',?,' ' ,'0',0,0,'0','0','0')";
		  Object[] params = new Object[] {user.getMember_name(),MD5.MD5Encode(user.getPassword()),user.getNickname(),
				  user.getEmail(),user.getMobile(),user.getAvatar(),StringUtil.getTimeSimple()};

		return getDAO().getJdbcTemplate().update(sql,params);
	}

	@Override
	public int addMessage(ShopgooMessage msg) throws TransactionException {
		// TODO Auto-generated method stub
		String sql ="insert into shopgoo_message (mobile,time,type,code) values (?,?,?,?)";
		  Object[] params = new Object[] {msg.getMobile(),msg.getTime(),msg.getType(),msg.getCode()};
		  
		return getDAO().getJdbcTemplate().update(sql,params);
	}

	@Override
	public ShopgooMessage queryMessage(String phone) throws TransactionException {
		// TODO Auto-generated method stub
		String sql ="select *  from shopgoo_message  where mobile='"+phone+"' ORDER BY time desc  LIMIT 1";
		//System.err.println(sql);
		SqlRowSet rs= getDAO().getJdbcTemplate().queryForRowSet(sql);
		ShopgooMessage mem =null;
		while(rs.next()){
			mem =new ShopgooMessage();
			mem.setMsgid(rs.getInt("msgid"));
			mem.setCode(rs.getString("code"));
			mem.setTime(rs.getInt("time"));
			mem.setType(rs.getInt("type"));
			mem.setMobile(rs.getString("mobile"));
		}
		
		return 	mem;
	}

	@Override
	public int updateKey(String key, String value, String id)
			throws TransactionException {
		String sql= "update   shopgoo_member set "+key+"='"+value+"' where member_id="+id;
		//System.err.println(sql);
		getDAO().getJdbcTemplate().execute(sql);
		return 0;
	}

	@Override
	public int checkPhoneReg(String phone){
		// TODO Auto-generated method stub
		return getDAO().getJdbcTemplate().queryForInt("select count(*)  as c from shopgoo_member  where mobile='"+phone+"'");
	}

	@Override
	public ShopgooMember getByUserName(String username)
			throws TransactionException {
		String sql ="SELECT  * from shopgoo_member where member_name='"+username+"'";
		JdbcTemplate jb =getDAO().getJdbcTemplate();
		List<ShopgooMember> list=  jb.query(sql, new MemberMapper());
		if(list.size()==0){
			return null;
		}
		return list.get(0);
	}



}
